public class DynArrayQueue {
    int front;
    int rear;
    int capacity;
    int array[];

    /**
     * 动态循环队列初始化
     */
    public DynArrayQueue(){
        this.front = -1;
        this.rear = -1;
        this.capacity = 1;
        array = new int[1];
    }

    boolean isEmpty(){
        return (front == -1);
    }

    boolean isFull(){
        return ((rear + 1) % capacity == front);
    }

    /**
     * 队列扩容，将初始队列扩容两倍
     */
    void resizeQueue(){
        int initCapacity = capacity;
        capacity = capacity * 2;
        int[] oldArray = array;
        array = new int[this.capacity];
        //将初始队列数组复制到新数组中
        for(int i = 0; i < oldArray.length; i++){
            array[i] = oldArray[i];
        }
        if(rear < front){
            //当rear<front时，将前面的rear元素移动到后面
            for(int i = 0; i < front; i++){
                array[i + initCapacity] = this.array[i];
                array[i] = Integer.parseInt(null);
            }
            rear = rear + initCapacity;

        }
    }

    /**
     * 在队尾插入一个数据
     * @param data:插入的数据
     */
    void enDynQueue(int data){
        if(isFull()){
            resizeQueue();
        }else{
            rear = (rear + 1) % capacity;
            array[rear] = data;
            if(front == -1){
                front = rear;
            }
        }
    }

    /**
     * 删除并返回队列头元素
     * @return
     */
    int deQueue(){
        int data = 0;
        if(isEmpty()){
            System.out.println("队列为空");
        }else{
            data = array[front];
            if(front == rear){
                front = rear - 1;
            }else{
                front = (front + 1) % capacity;
            }
        }
        return data;
    }

    int getDynQueueSize(){
        return ((capacity - rear + front + 1) % capacity);
    }

}


class DynArrayQueueTest{
    public static void main(String[] args) {
        DynArrayQueue dynArrayQueue = new DynArrayQueue();
        dynArrayQueue.enDynQueue(2);;
        dynArrayQueue.enDynQueue(4);
        dynArrayQueue.enDynQueue(3);
        dynArrayQueue.enDynQueue(5);
        //arrayQueue.enQueue(5);
        dynArrayQueue.deQueue();
        dynArrayQueue.deQueue();
        for(int i = 0;i < dynArrayQueue.getDynQueueSize(); i++){
            System.out.println(dynArrayQueue.array[i]);
        }

    }
}